package com.version1.diabeco.ui.comida;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.version1.diabeco.R;
import com.version1.diabeco.model.comida.DetalleComida;

public class DetalleComidaAdapter extends ArrayAdapter<DetalleComida> {
	private final Context context;
	final List<DetalleComida> values;

	public DetalleComidaAdapter(Context context, List<DetalleComida> values) {
		super(context, R.layout.activity_detalle_comida_fila, values);
		this.context = context;
		this.values = values;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.activity_detalle_comida_fila, parent, false);

		DetalleComida detalleComida = values.get(position);

		TextView textView = (TextView) rowView.findViewById(R.id.textViewAlimentoDetalleComida);
		textView.setText(detalleComida.getAlimento().getNombre());

		textView = (TextView) rowView.findViewById(R.id.textView2);
		textView.setText(detalleComida.getAlimento().getCarbohidratos().toString());

		TextView hdcAjustado = (TextView) rowView.findViewById(R.id.textView4);
		hdcAjustado.setText(detalleComida.getCarbohidratosAjustados().toString());

		ImageButton delete = (ImageButton) rowView.findViewById(R.id.deleteRowButton);
		delete.setOnClickListener(new DeleteButtonListener(values, position));
		
		SeekBar sk = (SeekBar) rowView.findViewById(R.id.seekBar1);

		// el valor maximo del progress bar es hasta el doble de los carbohidratos
		int max = new Double(detalleComida.getAlimento().getCarbohidratos() * 2).intValue();
		if (max == 0) max = 20; // si es 0, te da un maximo de 20 gramos de carbohidratos.
		
		int progress = detalleComida.getCarbohidratosAjustados().intValue();
		sk.setMax(max);
		sk.setProgress(progress);
		
		DetalleComidaFilaAdapterSeekBarListener seekBarListener = new DetalleComidaFilaAdapterSeekBarListener(values, position);

		sk.setOnSeekBarChangeListener(seekBarListener);
		
		return rowView;
	}
}

class DeleteButtonListener implements OnClickListener {
	
	private int position;
	final List<DetalleComida> values;

	DeleteButtonListener(List<DetalleComida> values, int position) {
		this.values = values;
		this.position = position;
	}

	@Override
	public void onClick(View imageButton) {

    	this.values.remove(position);
    	
    	RelativeLayout rl = (RelativeLayout) imageButton.getParent();
    	LinearLayout ll = (LinearLayout) rl.getParent();
    	LinearLayout ll2 = (LinearLayout) ll.getParent();
    	ListView listaDetalleComida = (ListView) ll2.getParent();
    	
		((DetalleComidaAdapter)listaDetalleComida.getAdapter()).notifyDataSetChanged();
    	
	}
	
}


class DetalleComidaFilaAdapterSeekBarListener implements OnSeekBarChangeListener {
	private int position;
	final List<DetalleComida> values;
	
	DetalleComidaFilaAdapterSeekBarListener(List<DetalleComida> values, int position) {
		this.values = values;
		this.position = position;
	}

	@Override       
    public void onStopTrackingTouch(SeekBar seekBar) {      
        // TODO Auto-generated method stub
    	System.out.println("");
    }

    @Override       
    public void onStartTrackingTouch(SeekBar seekBar) {     
    	System.out.println("");
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {     

    	LinearLayout seekBarLayout = (LinearLayout)seekBar.getParent();
    	LinearLayout detalleComidaFilaLayout = (LinearLayout)seekBarLayout.getParent();
    	TextView hdcAjustado = (TextView) detalleComidaFilaLayout.findViewById(R.id.textView4);
    	hdcAjustado.setText(progress + ""); 
    	this.values.get(position).setCarbohidratosAjustados(new Long(progress));
    	
    	LinearLayout layout = (LinearLayout)detalleComidaFilaLayout.getParent().getParent();
    	layout.getChildCount();
    	((LinearLayout)layout.getChildAt(1)).findViewById(R.id.textView4);
    	
    	ListView listView = ((ListView)layout.getChildAt(0));
    	Long hdcTotal = 0L;
    	for (int i = 0 ; i < listView.getChildCount() ; i++) {
    		TextView tv = (TextView) listView.getChildAt(i).findViewById(R.id.textView4);
    		hdcTotal += Long.valueOf(tv.getText().toString());
    		
    	}

    	try { 
	    	((ListView)layout.getChildAt(0)).findViewById(R.id.textView4);
	
	    	TextView textViewHdcTotalValor = (TextView) (layout).findViewById(R.id.textViewHdcTotalValor);
	    	textViewHdcTotalValor.setText(hdcTotal + "");
    	} catch(Exception e) {
    		System.out.println("Error en la configuracion del HdC total.");
    		e.printStackTrace();
    	}
    }
	
}